package com.dhl.fin.api.service.messageService;

import cn.hutool.core.date.DateUtil;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.common.util.StringUtil;
import com.dhl.fin.api.constant.RemindConstant;
import com.dhl.fin.api.domain.ExtraJobCron;
import com.dhl.fin.api.domain.RemindTask;
import com.dhl.fin.api.dto.TipMessageDTO;
import com.dhl.fin.api.service.messageService.superService.RemindTaskAbstract;

import java.util.Date;

/**
 * @author CuiJianbo
 * @since 2025/6/8
 */
public class LoanMessage extends RemindTaskAbstract {

    public LoanMessage(RemindTask remindTask, ExtraJobCron extraJobCron) {
        this.remindTask = remindTask;
        this.extraJobCron = extraJobCron;
    }

    @Override
    public TipMessageDTO tipMessage() {
        String tipMessage = "";
        if (StringUtil.isNotEmpty(extraJobCron.getRemark())) {
            if (extraJobCron.getRemark().equals(RemindConstant.ON_TIME_TIP)) {
                tipMessage = String.format("今天要还款%s元", remindTask.getLoadMoneyPerMonth());
            } else if (extraJobCron.getRemark().equals(RemindConstant.PRE_TIME_TIP)) {
                Date date = extraJobCronService.queryNextRemindTimeInCurrentCycle(remindTask.getId());
                if (ObjectUtil.isNull(date)) {
                    tipMessage = StringUtil.join("还有几天还款%s元", remindTask.getRemindName(), remindTask.getLoadMoneyPerMonth());
                } else {
                    long n = DateUtil.betweenDay(extraJobCron.getExecuteTime(), date, true);
                    if (n == 0) {
                        tipMessage = String.format("今天要还款%s元", remindTask.getLoadMoneyPerMonth());
                    } else if (n == 1) {
                        tipMessage = String.format("明天要还款%s元", remindTask.getLoadMoneyPerMonth());
                    } else {
                        tipMessage = String.format("倒计时提醒还款%s元", remindTask.getLoadMoneyPerMonth());
                    }
                }
            } else if (extraJobCron.getRemark().equals(RemindConstant.CYCLE_TIME_TIP)) {
                tipMessage = String.format("已经过了还款期，还款金额%s元", remindTask.getLoadMoneyPerMonth());
            }
        }

        return TipMessageDTO.builder()
                .openId(remindTask.getUnicode())
                .remindName(remindTask.getRemindName())
                .remindDescription(tipMessage)
                .remindType(remindTask.getRemindTypeName())
                .remainTime(extraJobCron.getRemainTime())
                .remindWayType(extraJobCron.getRemark())
                .build();

    }

}
